package com.kyd.push.common.utils;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * StrKit
 *
 * @author kejw on 2018/8/14
 */
public class StrKit {

	/**
	 * 格式化字符串 字符串中使用{key}表示占位符
	 *
	 * @param content 格式化字符串
	 * @param params 对象或Map<String, Object>类型的集合，其中Object类型为String\Number
	 * @return
	 */
	public static String format(String content, Map<String, Object> params) {
		String reg = "\\{(.*?)\\}";//
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(content);
		while (matcher.find()) {
			String group = matcher.group();//
			String key = group.substring(1, group.length() - 1);
			if (!params.containsKey(key)) {
				throw new RuntimeException("can not find key: " + key);
			}

			content = content.replace(group, String.valueOf(params.get(key)));
		}
		return content;
	}

	public static String getRandomUUID() {
		return java.util.UUID.randomUUID().toString().replace("-", "");
	}
}
